<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div {
        width: 200px;
        height: 200px;
        background-color: yellow;
      }
    </style>
  </head>
  <body>
    <div></div>
    <button>开始</button>
  </body>
  <script>
    window.onload = function () {
      function fn(begin, step, end, timer, second) {
        var btn = document.querySelector("button");
        var div = document.querySelector("div");

        btn.addEventListener("click", function () {
          timer = setInterval(function () {
            step += begin;
            div.style.marginLeft = step + "px";
            if (step >= end) {
              clearInterval(timer);
            }
          }, second);
        });
      }
      fn(10, 0, 600, null, 10);
    };
  </script>
</html>
